package org.jypj.dev.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.jypj.dev.dao.IWorkDao;
import org.jypj.dev.model.Work;
import org.jypj.dev.service.IWorkService;
import org.jypj.dev.util.Page;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Service;

@Service("workService")
public class WorkServiceImpl implements IWorkService {

    @Resource
    private IWorkDao workDao;


    /**
     * 保存 字段为空的不存防止覆盖存在默认值的字段
     *
     * @param work
     * @return 保存后的对象包括ID
     */
    public int saveWorkByField(Work work) {
        if (StringUtil.isEmpty(work.getId())) {
            String uuid = UUID.randomUUID().toString().replace("-", "").trim();
            work.setId(uuid);
        }
        return workDao.saveWorkByField(work);
    }

    /**
     * 保存 所有字段全都保存
     *
     * @param work
     * @return 保存后的对象包括ID
     */
    public int saveWork(Work work) {
        int code = 0;

        String id = work.getId();
        int catalogDepth = (id.length() - 1) / 2;
        work.setSortOrder(id);
        work.setCatalogDepth(catalogDepth);
        // 得到对应的车型并保存
        Map<String, String> trainTypeNames = work.getTrainTypeNames();
        String trainTypes = "";
        for (String trainType : trainTypeNames.keySet()) {
            trainTypes = trainTypes + trainType + ",";
        }
        work.setTrainType(trainTypes);
        workDao.saveWork(work);

        return code;
    }

    /**
     * 根据ID删除
     *
     * @param id 主键ID
     * @return 删除记录数
     */
    public int deleteWorkById(String id) {
        // 删除当前id包括子节点
        return workDao.deleteWorkById(id);
    }

    /**
     * 根据对象删除
     *
     * @return
     */
    public int deleteWorkByObject(Work work) {

        return workDao.deleteWorkByObject(work);
    }

    /**
     * 更新 只更新值不为空的字段
     *
     * @param work
     * @return 保存后的对象包括ID
     */
    public int updateWorkByField(Work work) {

        return workDao.updateWorkByField(work);
    }

    /**
     * 更新 更新所有字段
     *
     * @param work
     * @return 保存后的对象包括ID
     */
    public int updateWork(Work work) {
        int code = 0;

        // 得到对应的车型并保存
        Map<String, String> trainTypeNames = work.getTrainTypeNames();
        String trainTypes = "";
        for (String trainType : trainTypeNames.keySet()) {
            trainTypes = trainTypes + trainType + ",";
        }
        work.setTrainType(trainTypes);
        workDao.updateWork(work);

        return code;
    }

    /**
     * 移动工序
     */
    public int moveWork(String id1, String id2) {
        int code = 0;
        Work work1 = workDao.selectWorkById(id1);
        Work work2 = workDao.selectWorkById(id2);
        workDao.updateWorkSortOrder(id1, work2.getSortOrder());
        workDao.updateWorkSortOrder(id2, work1.getSortOrder());
        return code;
    }

    /**
     * 按ID查询
     *
     * @return Work
     * @parm id 主键ID
     */
    public Work selectWorkById(String id) {

        return workDao.selectWorkById(id);
    }

    /**
     * 查询子类节点中最大的ID值
     *
     * @return
     */
    public String selectMaxChildId(String parentId) {
        String maxId = workDao.selectMaxChildId(parentId);
        if (StringUtil.isEmpty(maxId)) {
            maxId = "0";
        }
        String newId = Integer.parseInt(maxId) + 1 + "";
        if (newId.length() == 1) {
            newId = "0" + newId;
        }
        return newId;
    }

    /**
     * 分页查询 包含条件
     *
     * @param page 分页对象
     * @param map  查询条件
     * @return List<Work>
     */
    public Page selectOnePageByMap(Page page, Map<String, Object> map) {
        List<Work> works = workDao.selectOnePageByMap(page, map);
        if (works != null && works.size() > 0) {
            page.setResult(works);
        } else {
            page.setResult(new ArrayList<Work>());
        }
        return page;
    }

    /**
     * 分页查询 包含对象条件
     *
     * @param page 分页对象
     * @param work 查询条件
     * @return Page
     */
    public Page selectOnePageByWork(Page page, Work work) {
        List<Work> works = workDao.selectOnePageByWork(page, work);
        if (works != null && works.size() > 0) {
            page.setResult(works);
        } else {
            page.setResult(new ArrayList<Work>());
        }
        return page;
    }

    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<Work>
     */
    public List<Work> selectAllByMap(Map<String, Object> map) {
        return workDao.selectAllByMap(map);
    }

    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<Work>
     */
    public List<Work> selectAllByWork(Work work) {

        return workDao.selectAllByWork(work);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return Work
     */
    public Work selectObjectByMap(Map<String, Object> map) {

        return workDao.selectObjectByMap(map);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return Work
     */
    public Work selectObjectByWork(Work work) {

        return workDao.selectObjectByWork(work);
    }
}